package com.hbsx.why.ui.space;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hbsx.why.NormalMainActivity;
import com.hbsx.why.R;
import com.hbsx.why.TabMainActivity;
import com.hbsx.why.adapter.LGRecycleViewAdapter;
import com.hbsx.why.adapter.LGViewHolder;
import com.hbsx.why.base.BaseFragment;
import com.hbsx.why.utils.PopMenu;
import com.hbsx.why.utils.refresh.PullLinearLayout;
import com.hbsx.why.utils.refresh.PullRecycleView;
import com.hbsx.why.utils.refresh.PullScrollView;
import com.hbsx.why.utils.refresh.PullToRefreshLayout;
import com.hbsx.why.utils.refresh.listener.ScrollViewListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class SpaceChildFragment extends BaseFragment {
    public static TabMainActivity myTabActivity;
    @BindView(R.id.my_PullScrollView)
    PullScrollView myPullScrollView;
    @BindView(R.id.my_PullToRefreshLayout)
    PullToRefreshLayout myPullToRefreshLayout;
    @BindView(R.id.no_date)
    PullToRefreshLayout noDatePullToRefreshLayout;
    @BindView(R.id.no_date_linearLayout)
    PullLinearLayout noDateLinearLayout;
    @BindView(R.id.my_PullRecycleView)
    PullRecycleView myPullRecycleView;
    @BindView(R.id.item_fragment_sroll_linear)
    LinearLayout item_fragment_sroll_linear;
    private static String mTitle;
    private static String BUNDLE_flag = "BUNDLE_flag";
    @BindView(R.id.area_text)
    TextView areaText;
    @BindView(R.id.shun_text)
    TextView shunText;
    @BindView(R.id.xuan_text)
    TextView xuanText;
    private int flag;
    public static NormalMainActivity myNormalMainActivity;
    private List<String> stringList;
    private int page = 0;
    private Context context;
    private MyAdapter myAdapter;
    private int[] location = new int[2];
    private int[] location2 = new int[2];
    private boolean changeVisible = true, isInitData = false;
    private PopMenu<String> popMenu;

    @Override
    protected int getLayout() {
        return R.layout.ui_space_child_fragment;
    }

    @Override
    protected void onInit() {
        context = getActivity();
        Bundle arguments = getArguments();
        if (arguments != null) {
            flag = arguments.getInt(BUNDLE_flag);
        }
        noDateLinearLayout.setPullDownEnable(true);
        noDateLinearLayout.setPullUpEnable(false);
        stringList = new ArrayList<>();
        if (stringList.size() > 0) {
            noDatePullToRefreshLayout.setVisibility(View.GONE);
        } else {
            noDatePullToRefreshLayout.setVisibility(View.VISIBLE);
        }
        myAdapter = new MyAdapter(stringList);
        setGridLayout(myPullRecycleView, context, 1);
        myPullRecycleView.setAdapter(myAdapter);
        item_fragment_sroll_linear.setVisibility(View.GONE);
        myPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        getData(page);
                        myAdapter = new MyAdapter(stringList);
                        setGridLayout(myPullRecycleView, context, 1);
                        myPullRecycleView.setAdapter(myAdapter);
                        myPullToRefreshLayout.refreshFinish(true);
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = page + 1;
                        getData(page);
                        myAdapter.addDate(stringList, true);
                        myAdapter.notifyDataSetChanged();
                        myPullToRefreshLayout.loadMoreFinish(true);
                    }
                }, 1000);
            }
        });
        noDatePullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        getData(page);
                        myAdapter = new MyAdapter(stringList);
                        setGridLayout(myPullRecycleView, context, 1);
                        myPullRecycleView.setAdapter(myAdapter);
                        noDatePullToRefreshLayout.refreshFinish(true);
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {

            }
        });
        myPullScrollView.setScrollViewListener(new ScrollViewListener() {
            @Override
            public void onScrollChanged(PullScrollView scrollView, int x, int y, int oldx, int oldy) {
                item_fragment_sroll_linear.getLocationOnScreen(location);
                myAdapter.getLinearLayout().getLocationOnScreen(location2);
                if (location[1] > location2[1] && changeVisible) {
                    changeVisible = false;
                    myAdapter.getLinearLayout().setVisibility(View.GONE);
                    item_fragment_sroll_linear.setVisibility(View.VISIBLE);
                } else if (location[1] < location2[1] && !changeVisible) {
                    changeVisible = true;
                    myAdapter.getLinearLayout().setVisibility(View.VISIBLE);
                    item_fragment_sroll_linear.setVisibility(View.GONE);
                }
                // showToast("-111-" + location[0] + "--" + location[1] + "-222-" + location2[0] + "--" + location2[1]);
            }
        });

        myAdapter.setOnItemClickListener(R.id.text_view_img1,new Adaperitemclick());
        myAdapter.setOnItemClickListener(R.id.text_view_img2,new Adaperitemclick());
        myAdapter.setOnItemClickListener(R.id.text_view_img3,new Adaperitemclick());
        myAdapter.setOnItemClickListener(R.id.text_view_img4,new Adaperitemclick());
        myAdapter.setOnItemClickListener(R.id.image_view,new Adaperitemclick());
    }

    class Adaperitemclick implements LGRecycleViewAdapter.ItemClickListener {

        @Override
        public void onItemClicked(View view, int position) {
            startActivity(new Intent(context,VenueDetailActivity.class));
        }
    }

    @Override
    public void fetchData() {
        if (!isInitData) {
            page = 1;
            getData(page);
            myAdapter.refreshDate(stringList);
            myAdapter.notifyDataSetChanged();
        }
    }

    private void getData(int page) {
        if (page == 1) {
            stringList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                if (i % 5 == 0) {
                    stringList.add("安庆市图书馆");
                }
                if (i % 5 == 1) {
                    stringList.add("安庆市文化馆");
                }
                if (i % 5 == 2) {
                    stringList.add("桐城市文化馆");
                }
                if (i % 5 == 3) {
                    stringList.add("安庆市美术馆");
                }
                if (i % 5 == 4) {
                    stringList.add("安庆博物馆");
                }
            }
            if (stringList.size() > 0) {
                noDatePullToRefreshLayout.setVisibility(View.GONE);
            } else {
                noDatePullToRefreshLayout.setVisibility(View.VISIBLE);
            }
        } else {
            stringList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                stringList.add(flag + "加载数据--" + page + i);
            }
        }
    }


    @OnClick({R.id.area_text, R.id.area_img, R.id.shun_text, R.id.shun_img, R.id.xuan_text, R.id.xuan_img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.area_text:
                initPop(areaText, myAdapter.getAreaText());
                break;
            case R.id.area_img:
                initPop(areaText, myAdapter.getAreaText());
                break;
            case R.id.shun_text:
                initPop(shunText, myAdapter.getShunText());
                break;
            case R.id.shun_img:
                initPop(shunText, myAdapter.getShunText());
                break;
            case R.id.xuan_text:
                initPop(xuanText, myAdapter.getXuanText());
                break;
            case R.id.xuan_img:
                initPop(xuanText, myAdapter.getXuanText());
                break;
        }
    }

    public class MyAdapter extends LGRecycleViewAdapter<String> {
        private boolean flag = false;
        private final int typetop = 1, typecenter = 2, typebottom = 3, nodata = 4;
        private LinearLayout linearLayout;
        private List<String> dataList;
        private TextView textViewlist, areaTextlist, shunTextlist, xuanTextlist;

        public TextView getAreaText() {
            return areaTextlist;
        }

        public TextView getShunText() {
            return shunTextlist;
        }

        public TextView getXuanText() {
            return xuanTextlist;
        }

        public MyAdapter(List<String> dataList) {
            super(dataList);
            this.dataList = dataList;

        }

        public LinearLayout getLinearLayout() {
            return linearLayout;
        }

        public void setLinearLayout(LinearLayout linearLayout) {
            this.linearLayout = linearLayout;
        }

        public void refreshDate(List<String> dataList, boolean flag) {
            super.refreshDate(dataList);
            this.flag = flag;
        }

        public void addDate(List<String> dataList, boolean flag) {
            super.addDate(dataList);
            this.flag = flag;
        }

        @Override
        public int getItemViewType(int position) {
            switch (position) {
                case 0:
                    position = typetop;
                    break;
                case 1:
                    position = typecenter;
                    break;
                case 2:
                    position = typebottom;
                    break;
                default:
                    position = typebottom;
                    break;
            }
            return position;
        }

        @Override
        public int getLayoutId(int viewType) {
            switch (viewType) {
                case typetop:
                    viewType = R.layout.item_space_list_top;
                    break;
                case typecenter:
                    viewType = R.layout.item_space_list_bottom;
                    break;
                case typebottom:
                    viewType = R.layout.item_space_list_bottom;
                    break;
                default:
                    viewType = R.layout.item_space_list_bottom;
                    break;
            }
            return viewType;
        }


        @Override
        public void convert(LGViewHolder holder, String s, int position) {
            switch (position) {
                case 0:
                    break;
                case 1:
                    textViewlist = (TextView) holder.getView(R.id.text_view);
                    textViewlist.setText(s);
                    linearLayout = (LinearLayout) holder.getView(R.id.item_top_sroll_linear);
                    linearLayout.setVisibility(View.VISIBLE);
                    areaTextlist = (TextView) holder.getView(R.id.area_text);
                    shunTextlist = (TextView) holder.getView(R.id.shun_text);
                    xuanTextlist = (TextView) holder.getView(R.id.xuan_text);
                    areaTextlist.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            initPop(areaText, areaTextlist);
                        }
                    });
                    shunTextlist.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            initPop(shunText, shunTextlist);
                        }
                    });
                    xuanTextlist.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            initPop(xuanText, xuanTextlist);
                        }
                    });
                    break;
                case 2:
                    textViewlist = (TextView) holder.getView(R.id.text_view);
                    textViewlist.setText(s);
                    break;
                default:
                    textViewlist = (TextView) holder.getView(R.id.text_view);
                    textViewlist.setText(s);
                    break;
            }
        }
    }


    public static SpaceChildFragment newInstance() {
        SpaceChildFragment fragment = new SpaceChildFragment();
        return fragment;
    }

    public static SpaceChildFragment newInstance(String title, int flag) {
        SpaceChildFragment fragment = new SpaceChildFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(BUNDLE_flag, flag);
        mTitle = title;
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * GridLayoutManager
     */

    public void setGridLayout(PullRecycleView pullRecycleView, Context mContext, int spanCount) {
        CustomGridLayoutManager gridLayoutManager = new CustomGridLayoutManager(mContext, spanCount);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        gridLayoutManager.setScrollEnabled(false);
        pullRecycleView.setLayoutManager(gridLayoutManager);
    }

    public class CustomGridLayoutManager extends GridLayoutManager {
        private boolean isScrollEnabled = true;

        public CustomGridLayoutManager(Context context, int spanCount) {
            super(context, spanCount);
        }

        public void setScrollEnabled(boolean flag) {
            this.isScrollEnabled = flag;
        }

        @Override
        public boolean canScrollVertically() {
            //Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
            return isScrollEnabled && super.canScrollVertically();
        }
    }

    private void initPop(final TextView textView, final TextView textViewlist) {
        popMenu = new PopMenu<String>(context, stringList) {
            @Override
            public int getLayoutManu() {
                return R.layout.layout_pomenu;
            }

            @Override
            protected int getmLayoutId() {
                return R.layout.item_pomenu;
            }

            @Override
            protected void binderconvert(LGViewHolder holder, final String s, int position) {
                TextView textview = (TextView) holder.getView(R.id.popwin_textView);
                textview.setText(s);
                textview.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        textView.setText(s);
                        textViewlist.setText(s);
                        popMenu.dismiss();
                    }
                });
            }
        };
        if (textView.getVisibility() == View.GONE) {
            popMenu.showAsDropDown(textViewlist);
        } else {
            popMenu.showAsDropDown(textView);
        }

    }
}
